<?php
/*
 * @Author: 逅来
 * @Description: 
 * @Date: 2023-03-19 14:55:20
 * @LastEditTime: 2023-03-28 21:08:30
 * @FilePath: \zhaopian.com\application\api\controller\photo\Route.php
 * @email: 934749708@qq.com
 */



namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\photo\Route as RouteModel;
use app\admin\model\Admin;
use think\Db;

class Route extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    /**
     * @description: 行程路线
     * @return {*}
     * @author: 逅来
     */
    public function select()
    {
        $list = RouteModel::field('id as route_id,name')->select();
        $this->success('行程路线', $list);
    }
    public function index(){
        $user_id = $this->auth->id;
        $admin_id = Admin::where('user_id',$user_id)->value('id');
        $per_page = $this->request->param('per_page',10);
        $page = $this->request->param('page',1);
        $total = Db::name('Broadcast')->where('admin_id',$admin_id)->count();
        $list = Db::name('Broadcast')->where('admin_id',$admin_id)->page($page,$per_page)->select();
        if(!empty($list)){
            foreach ($list as $key => &$value) {
                $value['image'] = cdnurl($value['image'])."?x-oss-process=image/resize,w_750";
                $value['route']=Db::name('PhotoRoute')->where('id',$value['route_id'])->value('name')?:'';
                
                $value['photo_num'] =  Db::name('BroadcastImagePhoto')->where('broadcast_id',$value['id'])->count('id');
            }
        }else{
            $list = [];
        }
        $this->success('主题', ['total'=>$total,'list'=>$list]);
    }
    public function detail(){
        $user_id = $this->auth->id;
        $id = $this->request->param('id');
        $detail = Db::name('Broadcast')->where('id',$id)->field('id,route_id,title,image,date,address,zan,browse,cover')->find();
        $image = Db::name('BroadcastImage')->where('broadcast_id',$id)->field('id,title')->select();
        if(!empty($image)){
            foreach ($image as $key => &$value) {
                $value['title']= date('n.d',strtotime($value['title']));
            }
            array_unshift($image,[
                'id'=>0,
                'title'=>'全部',
            ]);
        }else{
            $image =[   [
                'id'=>0,
                'title'=>'全部',
            ]];
        }
        if(!empty($detail)){
            $route = Db::name('PhotoRoute')->where('id',$detail['route_id'])->find();
            $detail['route'] = $route['name'];
            $detail['image'] = cdnurl($detail['image'])."?x-oss-process=image/resize,w_750";
            $detail['cover'] = cdnurl($detail['cover'])."?x-oss-process=image/resize,w_750";
            $detail['photo_num'] =  Db::name('BroadcastImagePhoto')->where('broadcast_id',$id)->count('id');
            $detail['photo_cate']=$image;
        }
        $this->success('主题详情', $detail);
    }
    public function images(){
        //$user_id = $this->auth->id;
        $id = $this->request->param('broadcast_id');
        $cate_id = $this->request->param('cate_id',0);
        $page = $this->request->param('page',1);
        $pagesize = $this->request->param('pagesize',10);
        $map['broadcast_id'] = $id;
        if($cate_id >0){
            $map['broadcast_image_id'] = $cate_id;
        }
        $total = Db::name('BroadcastImagePhoto')->where($map)->count();
        $image = Db::name('BroadcastImagePhoto')->where($map)->order('id desc')->field('id,image')->page($page,$pagesize)->select();
        if(!empty($image)){
            foreach ($image as $key => &$value) {  
                $value['source'] =$value['image'];
                $value['thumb'] = cdnurl($value['image'])."?x-oss-process=image/resize,w_150";
                $value['image'] = cdnurl($value['image'])."?x-oss-process=image/resize,w_750";
            }
    
        }else{
            $image = [];
        }

        $this->success('主题图片列表', ['total'=>$total,'list'=>$image]);
    }
    public function video(){
        $user_id = $this->auth->id;
        $id = $this->request->param('id');
        $page = $this->request->param('page',1);
        $pagesize = $this->request->param('pagesize',10);
        $video = Db::name('BroadcastVedio')->where('broadcast_id',$id)->field('id,vedio_title as title,vedio')->page($page,$pagesize)->select();
        $data = [];
        if(!empty($video)){
            foreach ($video as $key => $value) {
                $data[$key]['title'] =$value['title'];
                $data[$key]['url'] =cdnurl($value['vedio']);
            }
        }
        $this->success('主题视频列表',$data);
    }
    public function add(){
        $user_id = $this->auth->id;
        $admin_id = Admin::where('user_id',$user_id)->value('id');
        $route_id = $this->request->param('route_id',0);
        $title = $this->request->param('title','');
        if(empty($route_id)){
            $this->error('请选择行程路线');
        }
        if(empty($title)){
            $this->error('标题为空');
        }
        $route = RouteModel::where('id',$route_id)->find();
        $broadcast = \app\admin\model\Broadcast::create([
            'title'=>$title,
            'route_id'=>$route_id,
            'cover'=>$route['cover'],
            'image'=>$route['image'],
            'user_id'=>$user_id,
            'admin_id'=>$admin_id,
            'zan'=>0,
            'browse'=>0,
        ],true);
        $this->success('照片主题创建成功',$broadcast);
    }
    public function date(){
        $user_id = $this->auth->id;
        $admin_id = Admin::where('user_id',$user_id)->value('id');
        $broadcast_id = $this->request->param('broadcast_id');
        $title = $this->request->param('title');
        $count = Db::name('BroadcastImage')->where('broadcast_id',$broadcast_id)->max('weigh');
        $broadcast_image = \app\admin\model\BroadcastImage::create([
            'title'=>$title,
            'broadcast_id'=>$broadcast_id,
            'photo_num'=>0,
            'weigh'=>$count?($count+1):1,
        ],true);
        $this->success('照片主题日期列创建成功',$broadcast_image);
    }
    public function photo(){
        $url = $this->request->param('url');
        $broadcast_id = $this->request->param('date_id');
        $broadcast_image = Db::name('BroadcastImage')->where('id',$broadcast_id)->find();

        $image = str_ireplace('https://oss.bigchun.com','',$url);
        $photo =\app\admin\model\BroadcastImagePhoto::create([
            'broadcast_id'=>$broadcast_image['broadcast_id'],
                'broadcast_image_id'=>$broadcast_image['id'],
                'image'=> $image,
        ]);
        $photo['source'] =  $photo['image'];
        $photo['source'] =  cdnurl($photo['image'])."?x-oss-process=image/resize,w_750";
        $photo['thumb'] =  cdnurl($photo['image'])."?x-oss-process=image/resize,w_150";

        $this->success('添加成功',$photo);
    }
    public function delete(){
        $userid = $this->auth->id;
        $id = $this->request->param('ids');
        if(empty($id)){
            $this->error('参数错误');
        }
        Db::name('BroadcastImagePhoto')->where('id','in',$id)->delete();
        $this->success('直播图片删除成功');  
    }
    
    public function del(){
        $userid = $this->auth->id;
        $id = $this->request->param('id');
        
        if(empty($id)){
            $this->error('参数错误');
        }
        Db::name('Broadcast')->where('id',$id)->delete();
        $this->success('照片直播删除成功');  
    }
}
